﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
namespace IHISCETeim.DAL
{
    public class UClaimDemageDAL
    {
        public static int Add(UClaimDemageInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_Add",obj.DocEntry,obj.SerialNumber,obj.ClaimsNumber,obj.ItemCode,obj.Dscription,obj.Quantity,obj.Price,obj.LineTotal,obj.U_ExpressMode,obj.U_InvoiceNum,obj.AgentName,obj.U_Consignee,obj.U_Fuzeren,obj.TaxDate,obj.Remark,obj.U_OutType,obj.U_OutTypeText,obj.U_WSFLS,obj.UClaimType,obj.InStock);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_Delete", ID);
            try
            {
                DB.ExecuteScalar(dbCommand); return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetById(int ID)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_GetById", ID);
            return DB.ExecuteReader(dbCommand);
	    }
        public static DataTableCollection GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String,WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
            DataTableCollection dt = DB.ExecuteDataSet(dbCommand).Tables;
				
            TotalCount=Convert.ToInt32( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        public static bool UpdateInStock(int ID, bool InStock, DateTime InDate, decimal InQuantity, string InOrderNum, string InRemark, decimal InWuPan, bool IsPan, int InGuiFan, string GoTrack, string GOTrackDate)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_UpdateInStock", ID, InStock, InDate, InQuantity, InOrderNum, InRemark, InWuPan, IsPan, InGuiFan, GoTrack, GOTrackDate);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        /// <summary>
        /// 更新预计返回日期
        /// 规则:10号之前发货的在30提醒,10到30号发货的在下个月30号提醒
        /// </summary>
        public static void UpdateInWillDate()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_UpdateInWillDate");
            try
            {
                DB.ExecuteScalar(dbCommand);
              
            }
            catch (Exception exc)
            {
               
            }
        }
        /// <summary>
        /// 获取旧件未完全返回的记录提醒
        /// </summary>
        /// <returns></returns>
        public static DataTable GetInWillAlermList()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_GetInWillAlermList");
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
        /// <summary>
        /// 获取旧件索赔统计报表
        /// </summary>
        /// <param name="fromdate"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public static DataTableCollection GetTongJiReport(string fromdate, string todate)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_GetTongJiReport", fromdate, todate);
            return DB.ExecuteDataSet(dbCommand).Tables;
        }
        /// <summary>
        /// 获取代理商旧件索赔统计报表
        /// </summary>
        /// <param name="fromdate"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public static DataTableCollection GetAgentTongJiReportDetail(string agentname, string fromdate, string todate)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_GetAgentTongJiReportDetail",agentname, fromdate, todate);
            return DB.ExecuteDataSet(dbCommand).Tables;
        }

        /// <summary>
        /// 根据品情号获取所有索赔件
        /// </summary>
        /// <param name="ClaimsNumber"></param>
        /// <returns></returns>
        public static DataTable GetByClaimsNumber(string ClaimsNumber)
        {            
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_GetByClaimsNumber", ClaimsNumber);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }

        /// <summary>
        ///  根据物料代码，统计各物料的索赔情况，质管部要求
        /// </summary>
        /// <param name="fromdate"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public static DataTableCollection GetItemCodeTongJiReport(string fromdate, string todate)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_GetItemCodeTongJiReport", fromdate, todate);
            return DB.ExecuteDataSet(dbCommand).Tables;
        }
        /// <summary>
        /// 获取代理商的点检完成率报表
        /// </summary>
        /// <param name="fromdate"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public static DataTableCollection GetCheckReport(string fromdate, string todate)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_GetCheckReport", fromdate, todate);
            return DB.ExecuteDataSet(dbCommand).Tables;
        }
        /// <summary>
        /// 新机入库点检报表
        /// </summary>
        /// <param name="fromdate"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public static DataTableCollection NewCheckReport(string fromdate, string todate)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("UAgentMacStock_NewCheckReport", fromdate, todate);
            return DB.ExecuteDataSet(dbCommand).Tables;
        }

        /// <summary>
        /// 清理旧件
        /// </summary>
        /// <param name="AgentName"></param>
        /// <param name="SerialNumber"></param>
        /// <param name="ClaimsNumber"></param>
        /// <param name="ItemCode"></param>
        public static void ClearInWillAlerm(string AgentName, string SerialNumber, string ClaimsNumber, string ItemCode, string remark, string InQuantity)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_ClearInWillAlerm", AgentName, SerialNumber, ClaimsNumber, ItemCode, remark, InQuantity);
            DB.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 获取索赔件发货需通知列表
        /// 表1,为所有经销商列表
        /// 表2,为所有明细
        /// </summary>
        /// <returns></returns>
        public static DataTableCollection GetSPSendList()
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_GetSPSendList");
            return DB.ExecuteDataSet(dbCommand).Tables;
        }
        /// <summary>
        /// 更新索赔件发货短信已发送
        /// </summary>
        /// <param name="ID"></param>
        public static void UpdateSendSMS(int ID, bool sendmsg, string remark)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_UpdateSendSMS", ID, sendmsg, remark);
            try
            {
                DB.ExecuteScalar(dbCommand);

            }
            catch (Exception exc)
            {

            }
        }

        /// <summary>
        /// 旧件返回误判率（累计件数/返回件数）
        /// </summary>
        /// <param name="f"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static DataTable GetWuPaiBiLi(string f, string t)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_GetWuPaiBiLi", f, t);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }

        /// <summary>
        /// 旧件返回规范
        /// </summary>
        /// <param name="f"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static DataTable GetInGuiFan(string f, string t)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UClaimDemage_GetInGuiFan", f, t);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
       
    }
}
